<?php

class Thread_model extends MY_Model
{
    protected $_table = 'chatthread';   
    protected $primary_key = 'threadid';   
    
    function thread_by_id_($id){
    	$this->db->select("threadid,userName,agentName,agentId,lrevision,istate,ltoken,userTyping,agentTyping,unix_timestamp(dtmmodified) as modified, unix_timestamp(dtmcreated) as created,remote,referer,locale,lastpingagent, lastpinguser, unix_timestamp(lastpinguser) as lpuser,unix_timestamp(lastpingagent) as lpagent, unix_timestamp(CURRENT_TIMESTAMP) as current,nextagent,shownmessageid,userid,userAgent,groupid");
    	$this->db->where($this->primary_key, $id);		
    	return $this->db->get($this->_table)->row_array();
    }
    function commit_thread($threadid, $data){
    	$this->update($threadid, $data, TRUE);
    }
    function check_connections_from_remote($remote, $state_closed, $state_left)
    {
    	$this->db->where(array('remote' => $remote, 'istate <>' => $state_closed, 't.istate <>' => $state_left), FALSE);
    	return $this->db->count_all_results($this->_table. ' t');
    }
}
